package gui;

import collector.Configuration;
import parser.JavaRecognizer;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.util.HashMap;
import java.util.Map;

/**
 * @author dotekien
 *         Date: Mar 29, 2006
 *         Time: 11:04:13 AM
 */
public class EquateSelection extends JPanel implements ItemListener {
    private final HashMap<String, Integer> tokenMap = new HashMap<String, Integer>();
    private final Configuration configuration;

    public EquateSelection(Configuration configuration) {
        this.configuration = configuration;

        setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;

        c.gridx = 0;
        c.gridy = 0;
        JLabel label = new JLabel("Equate ");
        label.setToolTipText("The way to twist the equalilty among statements. See help page for more detail");
        add(label, c);

        int x = 0, y = 0;
        for (Map.Entry<Integer, Boolean> entry : configuration.getEquateTable().entrySet()) {
            c.gridx = x;
            c.gridy = 1 + y;
            y++;
            String name = JavaRecognizer._tokenNames[entry.getKey()];
            addCheckBox(name, KeyEvent.VK_0 + y, entry.getValue(), c, entry.getKey());
        }
    }

    public void addCheckBox(String name, int keyEvent, boolean status, GridBagConstraints position, int token) {
        JCheckBox cbox = new JCheckBox(name);
        cbox.setMnemonic(keyEvent);
        cbox.setSelected(status);
        cbox.addItemListener(this);

        tokenMap.put(name, token);
        add(cbox, position);
    }

    public void itemStateChanged(ItemEvent e) {
        if (e.getStateChange() == ItemEvent.SELECTED) {

            JCheckBox source = (JCheckBox) e.getItemSelectable();
            String key = source.getText();
            configuration.getEquateTable().put(tokenMap.get(key), source.isSelected());
        }
    }
}
